﻿/*
#------------------------------------------------------------------------------
#-- Program Name:	[dbo].[spRestoreGetAllBackups]
#-- Purpose:		Provides a mechanism query for a full list of database backups
#--	Last Update:	05/10/2012
#--					For a complete history - please review comments in SVN
#-- Called By:		Restore Database Utility Executable
#------------------------------------------------------------------------------
*/
CREATE PROCEDURE [dbo].[spRestoreGetAllBackups]
(
	@database_name		sysname,
	@start_date			datetime		= NULL,
	@label				nvarchar(255)	= NULL,
	@files_must_exist	bit				= 1,
	@recovery_window	bit				= 1
)
AS

SET NOCOUNT ON

IF LTRIM(RTRIM(@label)) = ''
	SET @label = NULL

IF @start_date IS NOT NULL
  BEGIN
	IF ISNULL(@files_must_exist, 0) = 0
		SELECT		*
		FROM		dbo.fncGetBackupList_Date(@database_name, @start_date) t1
		WHERE		ISNULL(name, '') = ISNULL(@label, ISNULL(name, ''))
	ELSE
		SELECT		*
		FROM		dbo.fncGetBackupList_Date(@database_name, @start_date) t1
		WHERE		file_exists = 1
					AND ISNULL(name, '') = ISNULL(@label, ISNULL(name, ''))
  END
ELSE IF ISNULL(@recovery_window, 1) = 1
  BEGIN
	IF ISNULL(@files_must_exist, 0) = 0
		SELECT		*
		FROM		dbo.fncGetBackupList_RecoveryWindow(@database_name) t1
		WHERE		ISNULL(name, '') = ISNULL(@label, ISNULL(name, ''))
	ELSE
		SELECT		*
		FROM		dbo.fncGetBackupList_RecoveryWindow(@database_name) t1
		WHERE		file_exists = 1
					AND ISNULL(name, '') = ISNULL(@label, ISNULL(name, ''))
  END
ELSE
  BEGIN
	SELECT		*
	FROM		dbo.fncBackupList(@database_name, NULL, NULL, @label) t1
  END

SET NOCOUNT OFF
